﻿using System;
using Text.IBll;
using Text.IDal;
using Text.Model;
using Text.Model.Shop;
using Text.Model.ShopModel;

namespace Text.Bll
{
    public class AdminBll : IAdminBll
    {
        IBaseDal<AdminModel> dal;
        IBaseDal<Address> Adddal;
        IBaseDal<ShopModel> ShopModeldal;

        public AdminBll(IBaseDal<AdminModel> dal, IBaseDal<Address> adddal, IBaseDal<ShopModel> shopModeldal)
        {
            this.dal = dal;
            Adddal = adddal;
            ShopModeldal = shopModeldal;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t">某个实体</param>
        /// <returns>受影响行数</returns>
        public async Task<int> ADD(AdminModel t)
        {
            var rec = await dal.GetValue(x => x.UserName == t.UserName);
            if(rec!=null)
            {
                return -1;
            }
            return await dal.ADD(t);
        }

      

        /// <summary>
        /// 显示列表
        /// </summary>
        /// <param name="exp">查询条件</param>
        /// <returns>返回显示的数据</returns>
        /// 
         
//=================================================================================================Shop
       /// <summary>
       /// 门店地址获取
       /// </summary>
       /// <param name="t"></param>
       /// <returns></returns>
       /// <exception cref="NotImplementedException"></exception>
        public async Task<int> ShopAdd(ShopModel t)
        {
            try
            {
                //1.门店名称不能重复
                var res = await ShopModeldal.GetValue(x => x.ShopName == t.ShopName && t.IsDeleted == false);
                if(res!=null)
                {
                    return -1;
                }
                //2.添加
                return await ShopModeldal.ADD(t);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 门店添加
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<Address> GetAddr()
        {
            return Adddal.GetList();
        }

        public IQueryable<AdminModel> GetModel(string? name="", int? min = null, int? max = null, DateTime? da1 = null, DateTime? da2 = null)
        {
            try
            {
                var list = dal.GetList().Where(x => x.UserState == false);
                if(!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x => x.UserName.Contains(name));
                }
                if(min!=null&&max!=null)
                {
                    list = list.Where(x => x.UserMoney >= min && x.UserMoney <= max);
                }
                if(da1!=null&&da2!=null)
                {
                    list = list.Where(x => x.UserTime >= da1 && x.UserTime <= da2.Value.AddDays(1));
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IQueryable<ShopDTO> GetShop(out int totalcount, out int totalpage, int pageindex = 1, int pagesize = 2, string? shopName = "", int? address = null)
        {
            //链表
            var list = ShopModeldal.GetList().Join(Adddal.GetList(), a => a.AddressId, b => b.Id, (a, b) => new ShopDTO
            {

                        ShopId                =a.ShopId,
                        ShopName              =a.ShopName,
                        BranchName            =a.BranchName,
                        ShopImg               =a.ShopImg,
                        OpenTime              =a.OpenTime,
                        State                 =a.State,
                        feature               =a.feature,
                        Desc                  =a.Desc,
                        AddressId             =a.AddressId,
                        HotelType             =a.HotelType,
                        CreateTime            =a.CreateTime,
                        UpdateTime            =a.UpdateTime,
                        IsDeleted             =a.IsDeleted,
                        AddressName           =b.AddressName

            }).Where(x => x.IsDeleted == false);
            // 查询
            if(!string.IsNullOrEmpty(shopName))
            {
                list = list.Where(x => x.ShopName.Contains(shopName));
            }
            //地址查询
            if(address!=null)
            {
                list = list.Where(x => x.AddressId == address);
            }
            //分页
            totalcount = list.Count();
            //计算总页数
            totalpage =(int)Math.Ceiling(totalcount * 1.0 / pagesize);
            list = list.OrderBy(x => x.AddressId).Skip((pageindex - 1) * pagesize).Take(pagesize);

            return list;
        }
    }
}
